package cn.finyzq.ss.games.web.ctrl;

import cn.finyzq.ss.games.vo.ResultBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static cn.finyzq.ss.games.utils.ResultCode.*;

/**
 * Create Time: 2018年04月25日 22:14
 *
 * @author fin
 */
@Controller
public class BaseCtrl {

    private Logger logger = LogManager.getLogger(getClass().getSimpleName());



    protected void catching(Throwable throwable) {
        logger.catching(throwable);
    }

    protected void debug(String s) {
        logger.debug(s);
    }

    protected void debug(String s, Object... objects) {
        logger.debug(s, objects);
    }

    protected void error(String s) {
        logger.error(s);
    }

    protected void error(String s, Object... objects) {
        logger.error(s, objects);
    }

    protected void error(String s, Throwable throwable) {
        logger.error(s, throwable);
    }

    protected void info(String s) {
        logger.info(s);
    }

    protected void info(String s, Object... objects) {
        logger.info(s, objects);
    }

    private ResultBean baseResponse(Object data, Integer status, String error){
        return new ResultBean(status, data, error);
    }

    public ResultBean success(Object data){
        return baseResponse(data, SUCCESS, null);
    }

    public ResultBean success(){
        return success(null);
    }

    public ResultBean err(Integer status, String error){
        return baseResponse(null, status, error);
    }

    public ResultBean err(Integer status) {
        return err(status, null);
    }

    public ResultBean err(String error){
        return err(NORMAL_ERROR, error);
    }

    public ResultBean err(){
        return err(NORMAL_ERROR);
    }

    private boolean isPaginationRequest(HttpServletRequest request) {
        String pageNoStr = request.getParameter("page");
        if (StringUtils.isBlank(pageNoStr)) {
            return false;
        } else {
            int pageNo = NumberUtils.toInt(pageNoStr, 0);
            return pageNo > 0;
        }
    }




}
